package com.lagou.filter;

import com.lagou.bean.MethodCallTime;
import org.apache.dubbo.common.constants.CommonConstants;
import org.apache.dubbo.common.extension.Activate;
import org.apache.dubbo.rpc.*;

import java.util.LinkedList;

@Activate(group = {CommonConstants.CONSUMER})
public class TPMonitorFilter implements Filter {

    public static LinkedList<MethodCallTime> queueA = new LinkedList<>();
    public static LinkedList<MethodCallTime> queueB = new LinkedList<>();
    public static LinkedList<MethodCallTime> queueC = new LinkedList<>();

    @Override
    public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException {
        // 当前系统时间
        long start_time = System.currentTimeMillis();
        try {
            // 执行方法
            return invoker.invoke(invocation);
        } finally {
            // 方法名称
            String methodName = invocation.getMethodName();
            // 当前系统时间
            long end_time = System.currentTimeMillis();
            // 保存到队列中
            if ("sayHelloA".equals(methodName)) {
                queueA.add(new MethodCallTime(start_time, end_time, (end_time - start_time)));
            } else if ("sayHelloB".equals(methodName)) {
                queueB.add(new MethodCallTime(start_time, end_time, (end_time - start_time)));
            } else if ("sayHelloC".equals(methodName)) {
                queueC.add(new MethodCallTime(start_time, end_time, (end_time - start_time)));
            }
//            System.out.println("方法："+methodName+"==》请求调用耗时：" + (end_time - start_time) + " 毫秒");
        }
    }
}
